package com.xinlan.xinlanmall.product.service.impl;

import com.xinlan.xinlanmall.product.entity.AttrEntity;
import com.xinlan.xinlanmall.product.service.AttrService;
import com.xinlan.xinlanmall.product.vo.AttrGroupWithAttrsVo;
import com.xinlan.xinlanmall.product.vo.AttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinlan.xinlanmall.common.utils.PageUtils;
import com.xinlan.xinlanmall.common.utils.Query;

import com.xinlan.xinlanmall.product.dao.AttrGroupDao;
import com.xinlan.xinlanmall.product.entity.AttrGroupEntity;
import com.xinlan.xinlanmall.product.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String)params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();

        if(StringUtils.isNotEmpty(key)){
            wrapper.and(obj -> {
                obj.lambda().eq(AttrGroupEntity::getAttrGroupId, key).or();
                obj.lambda().like(AttrGroupEntity::getAttrGroupName, key);
            });
        }

        IPage<AttrGroupEntity> page;
        if(catelogId == 0){
            page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
        } else{
            wrapper.lambda().eq(AttrGroupEntity::getCatelogId, catelogId);
            page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        }
        return new PageUtils(page);
    }

    /**
     * 根据分类id查询所有的分组以及这些组里面的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //根据分类id查询分组
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().lambda().eq(AttrGroupEntity::getCatelogId, catelogId));

        //查询所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, attrsVo);
            List<AttrEntity> relationAttrs = attrService.getRelationAttr(attrsVo.getAttrGroupId());
            attrsVo.setAttrs(relationAttrs);
            return attrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

}